﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicalManagementSystem.Models;
using MedicalManagementSystem.Utils;
using MedicalManagementSystem.Helpers;

namespace MedicalManagementSystem.Controllers
{
    public class RegionController : GenericController
    {
        //
        // GET: /Region/
        
        public ActionResult Index()
        {
            Menu.ID = "adm,region";
            return View(DbContext.Instance.Region);
        }

        //
        // GET: /Region/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Region/Create
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Region/Create
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "id")]Region region)
        {
            try
            {
                DbContext.Instance.AddToRegion(region);
                DbContext.Instance.SaveChanges();

                return RedirectToAction("Index");
                
            }
            catch (RuleViolationException ruleException)
            {
                AddViolataionsToModelState(ruleException);
            }
            return View(region);
        }
        
        //
        // GET: /Region/Edit/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Edit(int id)
        {
            var region = Region.GetByPK(id);
            return View(region);
        }

        //
        // POST: /Region/Edit/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Region region)
        {
            try
            {
                var original = Region.GetByPK(id);
                DbContext.Instance.ApplyPropertyChanges(original.EntityKey.EntitySetName, region);
                DbContext.Instance.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch (RuleViolationException ruleException)
            {
                AddViolataionsToModelState(ruleException);
            }
            return View(region);
        }

        
        //
        // POST: /Region/Delete/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(int id)
        {
            try
            {
                var region = Region.GetByPK(id);
                DbContext.Instance.DeleteObject(region);
                DbContext.Instance.SaveChanges(true);
            }
            catch (Exception e)
            {
                
            }

            return RedirectToAction("Index");
        }
    }
}
